package com.oa.hyperion.api.v1;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.oa.hyperion.entity.Interview;
import com.oa.hyperion.exception.http.NotFoundException;
import com.oa.hyperion.service.InterviewService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 面试controller
 */

//使用RestController =controller+ ResponseBody
@Api(value="面试controller",tags={"面试操作接口"})
@RestController
@RequestMapping("/v1/interview")
public class InterviewController {

    @Autowired
    private InterviewService interviewService;

    /**
     * 获取所有面试
     */
    @ApiOperation("获取所有面试")
    @GetMapping("getAll")
    public List<Interview> getAll(){
        return interviewService.list();
    }

    /**
     * 分页查询所有面试
     */
    @ApiOperation("分页查询所有面试")
    @GetMapping("getAllByPage")
    public Page<Interview> getAllByPage(Integer pageNum, Integer pageSize){
        //创建Page对象
        Page<Interview> employeePage = new Page<>(pageNum,pageSize);
        return interviewService.page(employeePage);
    }

    /**
     * 条件查询面试
     * 应聘者,岗位，地点
     */
    @ApiOperation("条件查询面试")
    @PutMapping("searchList")
    public List<Interview> getSearchListByCondition(@RequestBody Interview v){
        QueryWrapper<Interview> wrapper=new QueryWrapper<>();
        //以名字来查找
            //非空判断
        if(v.getApplicant()!=null){
            wrapper.like("applicant",v.getApplicant());
        }
            //以岗位
        if(v.getPost()!=null){
            wrapper.like("post",v.getPost());
        }
            //以地点来查找
        if(v.getAddress()!=null){
            wrapper.like("address",v.getAddress());
        }
        return interviewService.list(wrapper);
    }




    /**
     * 添加面试
     */
    @ApiOperation("添加面试")
    @PostMapping("add")
    public String addInterview(@RequestBody Interview interview){

        if(interviewService.save(interview)) {
            return "添加成功";
        }
        return "添加失败";
    }


    /**
     * 删除面试
     */
    @ApiOperation("删除面试")
    @DeleteMapping("delete")
    public String deleteInterview(Integer id){
       if(interviewService.removeById(id)) {
           return "删除成功";
       }
        return "删除失败";

    }

    /**
     * 修改面试
     */
    @ApiOperation("修改面试")
    @PutMapping("update")
    public String updateInterview(@RequestBody Interview interview){

        //获取要修改的对象
        Interview itv=interviewService.getById(interview.getId());

        //有效性校验
        if(itv==null){
            throw new NotFoundException(404);
        }
        //修改传过来的功能
        if(interview.getId()!=null){
            itv.setId(interview.getId());
        }

        if(interview.getApplicant()!=null){
            itv.setApplicant(interview.getApplicant());
        }

        if(interview.getResult()!=null){
            itv.setResult(interview.getResult());
        }

       if( interviewService.updateById(itv)){
           return "修改成功";
       }
        return "修改失败";
    }

}
